﻿/*** MikeWare Framework ************************************
* This class is part of product of MikeWare.Framework.
* 
* Author      : Mike Cheers
* Mail        : mikecheers@126.com
* Create Date : 2018/9/14
* Summary     : 
* 
* 
* Modified By : 
* Date        : 
* Mail        : 
* Comment     : 
**************************************************************/

namespace MikeWare.Core
{
    using System;
    using System.Collections;
    using System.Collections.Generic;

    public partial class Result<T> : IEnumerable<T>, IEnumerable, ICollection<T>, IDisposable
    {
        public Exception Exception { get; set; }

        private bool isSuccess = false;

        public bool IsSuccess
        {
            get { return isSuccess; }
            set { isSuccess = value; }
        }

        private string message = string.Empty;

        public string Message
        {
            get { return message; }
            set { message = value; }
        }

        public string ReturnCode { get; set; }

        public T FirstEntity
        {
            get
            {
                if (null != ResultList && ResultList.Count > 0) return ResultList[0];
                return default(T);
            }
        }

        public T SecondEntity
        {
            get
            {
                if (null != ResultList && ResultList.Count > 1) return ResultList[1];
                return default(T);
            }
        }

        private List<T> resultList = null;

        public List<T> ResultList
        {
            get
            {
                return resultList;
            }
            set
            {
                resultList = value;
                IsSuccess = (null != value && value.Count > 0);
            }
        }

        public void Clear()
        {
            if (null == ResultList) ResultList = new List<T>();
            else ResultList.Clear();
            IsSuccess = false;
            Message = string.Empty;
        }

        public void Add(T entity)
        {
            IsSuccess = true;
            if (null == ResultList) ResultList = new List<T>();
            ResultList.Add(entity);
        }

        public void Add(IEnumerable<T> entities)
        {
            if (null == ResultList) ResultList = new List<T>();

            if (null != entities) ResultList.AddRange(entities);

            IsSuccess = null != ResultList && 0 < ResultList.Count;
        }

        public bool Contains(T value)
        {
            if (null == ResultList)
                return false;
            return ResultList.Contains(value);
        }

        public Result()
        {
            resultList = new List<T>();
        }

        public Result(params T[] list)
        {
            resultList = new List<T>(list);
        }

        public T this[int i]
        {
            get
            {
                if (IsSuccess)
                    return resultList[i];
                else
                    return default(T);
            }
        }

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            if (IsSuccess) return resultList.GetEnumerator();
            return null;
        }

        #endregion

        public void CopyTo(T[] array, int arrayIndex)
        {
            //throw new System.NotImplementedException();
        }

        public int Count
        {
            get
            {
                if (null == ResultList) return 0;
                return ResultList.Count;
            }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(T item)
        {
            return ResultList.Remove(item);
        }

        IEnumerator<T> IEnumerable<T>.GetEnumerator()
        {
            if (IsSuccess) return resultList.GetEnumerator();
            return null;
        }

        public void CopyFrom(Result result)
        {
            Clear();
            if (null != result.Value && result.Value is T)
                Add((T)result.Value);
            IsSuccess = result.IsSuccess;
            Message = result.Message;
            ReturnCode = result.ReturnCode;
        }

        public void Merge(Result<T> returnModel)
        {
            if (null == returnModel || !(returnModel.IsSuccess))
                return;

            Add(returnModel.ResultList);
        }

        public void Dispose()
        {
            IsSuccess = false;
            resultList = null;
            Message = null;
        }
    }
}
